package com.itheima.dao.Impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAll() {
        Connection conn = null;
        Statement stat = null;
        ResultSet res = null;
        List<User> list = null;
        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
            //3.获取语句执行对象
            stat = conn.createStatement();
            //4.执行sql,返回结果集
            String sql = "select * from user";
            res = stat.executeQuery(sql);
            //5.处理结果集
            list = new ArrayList<>();
            while(res.next()){
                User user = new User();
                user.setId(res.getInt("id"));
                user.setName(res.getString("name"));
                user.setGender(res.getString("gender"));
                user.setAge(res.getInt("age"));
                user.setAddress(res.getString("address"));
                user.setQq(res.getString("qq"));
                user.setEmail(res.getString("email"));
                user.setUsername(res.getString("username"));
                user.setPassword(res.getString("password"));
                System.out.println("------");
                list.add(user);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    @Override
    public void add(User user) {
        Connection conn = null;
        Statement stat = null;

        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
            //3.获取语句执行对象
            stat = conn.createStatement();
            //4.执行sql,返回结果集
            String sql = "insert into user values (null,'"+user.getName()+"','"+user.getGender()+"','"+user.getAge()+"','" + user.getAddress() + "','" + user.getQq() + "','" + user.getEmail() + "',null,null)";
           stat.executeUpdate(sql);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
